<script src='<?php echo DIR_JS?>ui.datepicker.js' type='text/javascript' language='javascript'> </script>
<div class="section" id="sitemaplist">
  <div class="section-title">Quản lý thông tin đặt báo</div>
  <div class="section-content padding1">
    <form name="frm" id="frm" action="<?php echo $action?>" method="post" enctype="multipart/form-data">
    <input type="hidden" id="diachi" name="diachi" value="" class="text" />
      <div class="button right">
      	<a class="button save" onclick="save()">Lưu</a>
		<a class="button cancel" href="?route=deliman/khachhang">Bỏ qua</a>   
        <input type="hidden" name="khachhangid" value="<?php echo $item['khachhangid']?>">
        <input type="hidden" name="masokhachhang" value="<?php echo $item['masokhachhang'] ?>" />
      </div>
      
      <div class="clearer">^&nbsp;</div>
      <div id="error" class="error" style="display:none"></div>
      <div>
       <p>
          	<label>Khách hàng:</label>
         	<?php echo $item['hoten']?>
        </p>
        <p>
          <label>Địa chỉ:</label>
          <?php echo $item['diachi']?>
        </p>
        <p>
          <label>Email:</label>
		<?php echo $item['email']?>
        </p>
        <p>
          <label>Điện thoại:</label>
			<?php echo $item['dienthoai']?>
        </p>
        <p>
        	<a class="button add" id="themkybao">Thêm kỳ báo</a>
            <a class="button delete" id="btnDelRows" >Xóa dòng</a>   
        	
            <input type="hidden" id="selectkybao" name="selectkybao">
        </p>
        <table>
        	<thead>
            	 <tr class="tr-head">
                    <th width="1%"><input class="inputchk" type="checkbox" onclick="$('.inputchk').attr('checked', this.checked);"></th>   
					<th>Tên loại báo</th>	                   
                    <th>Tên kỳ báo</th>
                    <th width="10%">Số kỳ báo</th>
                    <th width="10%">Ngày phát hành</th>
                    <th width="10%">Ngày đặt báo</th>
                    <th width="10%">Số lượng</th>
                    <th>Giá bán</th>                             
                </tr>
            </thead>
            <tbody id="listkybao">
            </tbody>
        </table>
      </div>
    </form>
  </div>
</div>

<script language="javascript">
$(document).ready(function(){
	
});
var today = "<?php echo $this->date->formatMySQLDate($this->date->getToday()) ?>";
$('#themkybao').click(function(){
	var result = window.showModalDialog("index.php?route=deliman/kybao&dialog=true","", "dialogWidth:1000px; dialogHeight:800px; center:yes");
	
	var arr = $('#selectkybao').val().split(',');
	for(i=0;i<arr.length;i++)
	{
		//alert(arr[i]);
		if(arr[i]!="")
		{
			var flag = false;
			$('.kybaoid').each(function(){
				if(this.value == arr[i])
				{
					flag = true;	
				}
			});
			
			if(flag == false)
			{
				kybao.addRow("",arr[i],today,1);
			}
			else
			{
				alert('Bạn đã chọn kỳ báo này rồi');	
			}
		}
	}
});

$('#btnDelRows').click(function(){
	$('.rowitem').each(function(){
		
		if(this.checked == true)
		{
			//alert(this.value)
			$('#row'+this.value).remove();
		}
	});
	$('.inputchk').attr('checked', false);
});

function KyBao()
{
	this.index = 0;
	this.addRow = function(id,kybaoid,ngaydat, soluongdat)
	{
		$.getJSON("?route=deliman/kybao/getKyBao&col=kybaoid&val="+kybaoid, 
		function(data) 
		{
			for(i in data.kybaos)
			{
				//Du lieu post di
				var hidden_kybaoid = '<input type="hidden" class="kybaoid" id="kybaoid-'+ kybao.index +'" name="kybaoid['+ kybao.index +']" value="'+data.kybaos[i].kybaoid+'">'; 
				
				
				var cellchk = '<td><input type="checkbox" class="rowitem inputchk" value="'+ kybao.index +'"></td>';
				var cellloaibao = '<td><input type="hidden" name="id" value="'+id+'">'+data.kybaos[i].tenloaibao+'</td>';
				var celltenkybao = '<td>'+data.kybaos[i].tenkybao+'</td>';
				var cellngayphathanh = '<td>'+data.kybaos[i].ngayphathanh+'</td>';
				var cellsokybao = '<td class="number">'+data.kybaos[i].sokybao+'</td>';
				var cellngaydat = '<td><input type="text" class="text ben-datepicker"  id="ngaydat-'+ kybao.index +'" name="ngaydat['+ kybao.index +']" value="'+ngaydat+'"></td>';
				var cellsoluong = '<td class="number"><input type="text" class="text number" id="soluongdat-'+ kybao.index +'" name="soluongdat['+ kybao.index +']" value="'+soluongdat+'"></td>';
				var cellgiaban = '<td class="number">'+formateNumber( data.kybaos[i].giaban)+'</td>';
				
				var row = '<tr id="row'+ kybao.index +'">'
				row += 	cellchk;
				row +=	cellloaibao;
				row +=	celltenkybao;
				row +=	cellsokybao;
				row +=	cellngayphathanh;
				row += 	cellngaydat;
				row +=	cellsoluong;
				row +=	cellgiaban;	
				row +=	hidden_kybaoid;	
				row += '</tr>';
				$('#listkybao').append(row);
				kybao.index++;
				numberReady();
			}
			
		});
	}
	
}
var kybao = new KyBao();

function save()
{
	$.blockUI({ message: "<h1>Please wait...</h1>" });	
	$.post("?route=deliman/khachhang/savethongtindatbao", $("#frm").serialize(),
		function(data){
			if(data == "true")
			{
				window.location = "?route=deliman/khachhang";
				
			}
			else
			{
				$('#error').html(data).show('slow');
				$.unblockUI();	
			}
			
		}
	);
}

function deleteitem()
{
	$.post("?route=voucher/customerreturn/delete&orderid=<?php echo $orderid?>", 
			$("#formlist").serialize(), 
			function(data)
			{
				if(data!="")
				{
					window.location.reload();
					//window.close();
				}
			}
	);
}
</script>